<?php
mysql_connect('localhost', 'taras-race', 'mhxzkhl')  or die("Could not connect: " . mysql_error());
mysql_select_db('taras_race');
mysql_query('set names "uft8"');

if (isset($_GET['func'])) {
  if ($_GET['func'] === "check_player") {
    $owner = $_GET['owner'];
    $r = mysql_query(sprintf('select count(player_id) from owner where player_id = "%s"',
                             $owner));
    $r = mysql_fetch_array($r);
    echo $r[0];

  } else if($_GET['func'] === "insert") {
    $r = mysql_query($_POST['sql']);
    printf("%d rows inserted", mysql_affected_rows());

  } else if($_GET['func'] === "check_confilict") {
    $accounts = $_GET['accounts'];
    $claim = $_GET['claim'];

    foreach ($accounts as $account) {
      $sql = "select player_id from owner where account = '$account'";

      $ownedby = mysql_fetch_row(mysql_query($sql));

      if ($ownedby && count($ownedby) > 0 && $ownedby[0] != $claim) {
        echo "($ownedby[0])认为自己拥有($account)，并已经注册，如果这不是事实，请和管理员联系";
        return;
      }
    }
    echo "OK";
  } else {
    echo "unknown func type: " . $_GET['func'];
  }
  return;
}

$player_ids = mysql_query("select distinct(player_id) from owner");

?>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.3.0/build/cssreset/reset-min.css">
    <link rel="stylesheet" type="text/css" href="css/race.css">
    <title>Follower Race | Taras Team Internal</title>

    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript" src="js/sprintf.js"></script>
    <script type="text/javascript" src="js/race.js"></script>

  </head>
  <body>
    <pre id="intro">
      Please input your name and claim you IDs in the box below.
      Format:
      Email,Password

      For example:
      abc@gmail.com,123221
      fe@126.com,abedi213
    </pre>
    <hr/>
    <table>
    <tr>
    <td class="block-td" id="owner-info-block">
      <form action="#" method="POST" id="add-accounts">
        <table>
          <tr>
            <td><label for="owner">I am:</label>
              <input type="text" name="owner" size="6"/> </td>
            <td>
              <select name="owner-exist" onchange="update_owner(this)">
                <?php
                   while ($pid = mysql_fetch_row($player_ids)) {
                     echo "<option value='" . $pid[0] . "'>$pid[0]</option>";
                   }
                ?>
              </select>
            </td>
          </tr>
          <tr>
            <td> <input type="submit" value="提交这些ID到我的账号" id="submit"/> </td>
            <td> <input type="submit" value="查看我的所有ID的状态" id="submit" onclick="show_my_accounts(); return false;"/> </td>
          </tr>
        </tr>

        </table>
        <textarea name="accounts" cols="23.5" rows="32"></textarea> 
      </form>
    </td>

    <td class="block-td">
      <div id="info-div">
        <?php
           if(isset($_POST["owner"])) {
             // parse owner and each uname, passwd
             $owner = $_POST["owner"];
             echo "I am <span id='owner-span'>" . $owner . "</span>, and I own the accounts in below. Please <a id='add-to-db' href='#'>submit</a> them to DB<br/><br/>";
             echo "<table id='account-table'>";
             $accounts = explode("\n", $_POST["accounts"]);
             foreach ($accounts as $account) {
               $account = trim($account);
               if (strlen($account) > 0) {
                 $account = explode(',', $account);
                 echo sprintf("<tr><td class='cell'>%s</td><td class='cell'>%s</td></tr>", $account[0], $account[1]);
               }
             }
             echo "</table>";
           }
        ?>
      </div>
    </td>
    </tr>
    </table>
  </body>
</html>
